using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace GameLib.GameItem
{
    class GameItem
    {
        protected Texture2D Texture;
        protected Transition Trans;
        protected float TransTime;
        protected float Alpha = 0;
        protected int Duration = 0;
        protected ItemManager Manager;
        public bool Enable = true;

        public EventHandler ItemPress;

        public Rectangle ItemBound
        {
            get { return new Rectangle((int) position.X, (int) position.Y, Texture.Width, Texture.Height); }
        }

        public int ItemBoundRightPos
        {
            get { return ((int) position.X + Texture.Width); }
        }

        public int ItemBoundDownPos
        {
            get { return ((int)position.Y + Texture.Height); }
        }

        public Vector2 Position
        {
            get { return position; }
            protected set { position = value; }
        }
        Vector2 position;

        public GameItem(Texture2D texture, ItemManager man, Vector2 pos)
        {
            this.Texture = texture;
            this.Manager = man;
            this.position = pos;
            Trans = Transition.None;
        }

        public void FadeOut()
        {
            Trans = Transition.FadeOut;
            TransTime = 1000;
            Duration = 1000;
            Alpha = 1;
        }

        public void FadeIn()
        {
            Trans = Transition.FadeIn;
            TransTime = 0;
            Duration = 1000;
            Alpha = 0;
        }

        public virtual bool CheckPress(Vector2 touchPos)
        {
            return false;
        }

        public virtual void Clean()
        {
            Texture = null;
            Manager = null;
        }

        public virtual void Update(GameTime gametime)
        {
            switch (Trans)
            {
                case Transition.FadeOut:
                    TransTime -= gametime.ElapsedGameTime.Milliseconds;
                    if (TransTime <= 0)
                    {
                        Manager.RemoveItem(this);
                        Clean();
                    }
                    Alpha = TransTime / Duration;
                    break;

                case Transition.FadeIn:
                    TransTime += gametime.ElapsedGameTime.Milliseconds;
                    Alpha = TransTime / Duration;
                    if (Alpha > 1)
                    {
                        Trans = Transition.None;
                    }
                    break;
            }
        }

        public virtual void Draw(SpriteBatch spritebatch)
        {
            if (Trans != Transition.None)
            {
                spritebatch.Draw(Texture, position, Color.White * Alpha);
            }
            else
            {
                spritebatch.Draw(Texture, position, Color.White);
            }
        }
    }
}
